package com.cloud.animationtestdemo.strage

import android.animation.ValueAnimator
import android.graphics.Path
import android.graphics.PathMeasure
import android.view.View

/**
 * @author: njb
 * @date:   2025/1/3 21:19
 * @desc:   描述
 */
class BezierCurveAnimationStrategy :AnimationStrategy{
    override fun executeAnimation(view: View, duration: Long) {
        val path = Path()
        // 另一种二次贝塞尔曲线的控制点配置
        path.moveTo(0f, 0f)
        path.quadTo(50f, 450f, 500f, 0f)
        val pathMeasure = PathMeasure(path, false)
        val valueAnimator = ValueAnimator.ofFloat(0f, pathMeasure.length)
        valueAnimator.duration = duration
        valueAnimator.addUpdateListener { animation ->
            val value = animation.animatedValue as Float
            val pos = FloatArray(2)
            val tan = FloatArray(2)
            pathMeasure.getPosTan(value, pos, tan)
            view.x = pos[0]
            view.y = pos[1]
        }
        valueAnimator.start()
    }
}